home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / misc / unix / tracker_4_3.lzh / tracker / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-13  |  13.3 KB  |  600 lines

  1. /* commands.c 
  2.     vi:se ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: commands.c,v 4.0 1994/01/11 17:44:07 espie Exp espie $
  6.  * $Log: commands.c,v $
  7.  * Revision 4.0  1994/01/11  17:44:07  espie
  8.  * Abstracted IO.
  9.  *
  10.  * Revision 1.6  1994/01/09  17:36:22  Espie
  11.  * Generalized open.c.
  12.  *
  13.  * Revision 1.5  1994/01/08  02:04:21  Espie
  14.  * Some notice to status.
  15.  *
  16.  * Revision 1.4  1994/01/05  14:54:09  Espie
  17.  * *** empty log message ***
  18.  *
  19.  * Revision 1.3  1994/01/05  13:50:43  Espie
  20.  * Cosmetic change.
  21.  *
  22.  * Revision 1.2  1993/12/28  13:54:44  Espie
  23.  * Use notice.
  24.  *
  25.  * Revision 1.1  1993/12/26  00:55:53  Espie
  26.  * Initial revision
  27.  *
  28.  * Revision 3.15  1993/12/04  16:12:50  espie
  29.  * Lots of LOCAL added.
  30.  *
  31.  * Revision 3.14  1993/11/17  15:31:16  espie
  32.  * play_note instead of ch->mode.
  33.  *
  34.  * Revision 3.13  1993/08/04  11:34:33  espie
  35.  * *** empty log message ***
  36.  *
  37.  * Revision 3.12  1993/07/18  10:39:44  espie
  38.  * Cleaned up.
  39.  *
  40.  * Revision 3.11  1993/07/17  22:23:41  espie
  41.  * Fixed bug with bad loops.
  42.  *
  43.  * Revision 3.9  1993/05/09  14:06:03  espie
  44.  * Modified the way set_speed works.
  45.  *
  46.  * Revision 3.8  1993/04/28  20:13:13  espie
  47.  * Very small bug with volume (Lawrence).
  48.  *
  49.  * Revision 3.7  1993/01/15  14:00:28  espie
  50.  * Added bg/fg test.
  51.  *
  52.  * Revision 3.6  1992/11/27  10:29:00  espie
  53.  * General cleanup
  54.  *
  55.  * Revision 3.5  1992/11/24  10:51:19  espie
  56.  * More precise vibrato table.
  57.  *
  58.  * Revision 3.4  1992/11/23  10:12:23  espie
  59.  * *** empty log message ***
  60.  *
  61.  * Revision 3.3  1992/11/22  17:20:01  espie
  62.  * Simplified delay_pattern.
  63.  *
  64.  * Revision 3.2  1992/11/20  14:53:32  espie
  65.  * Added finetune.
  66.  *
  67.  * Revision 3.1  1992/11/19  20:44:47  espie
  68.  * Protracker commands.
  69.  *
  70.  * Revision 3.0  1992/11/18  16:08:05  espie
  71.  * New release.
  72.  *
  73.  * Revision 2.12  1992/11/13  13:24:24  espie
  74.  * Added some extended commands: E12AB, and some.
  75.  * now use set_volume in audio.c. All the device-dependent operation
  76.  * is there.
  77.  * Defensive programming: check the range of each note
  78.  * for arpeggio setup.
  79.  * Structured part of the code, especially replay ``automaton''
  80.  * and setting up of effects.
  81.  *
  82.  * Revision 1.9  1991/11/17  17:09:53  espie
  83.  * Added missing prototypes.
  84.  * Dynamic oversample and frequency.
  85.  * Added arpeggio.
  86.  * Fixed up vibrato depth.
  87.  * Added vibslide and portaslide.
  88.  * Added command 9.
  89.  */
  90.  
  91. #include <stdio.h>
  92.  
  93. #include "defs.h"
  94. #include "channel.h"
  95. #include "song.h"
  96. #include "extern.h"
  97.      
  98. ID("$Id: commands.c,v 4.0 1994/01/11 17:44:07 espie Exp espie $")
  99.  
  100. /* sine table for the vibrato effect (obtained through build.c) */
  101.  
  102. int vibrato_table[64] = 
  103.    {
  104.    0,50,100,149,196,241,284,325,362,396,426,452,473,490,502,510,512,
  105.    510,502,490,473,452,426,396,362,325,284,241,196,149,100,50,0,-49,
  106.    -99,-148,-195,-240,-283,-324,-361,-395,-425,-451,-472,-489,-501,
  107.    -509,-511,-509,-501,-489,-472,-451,-425,-395,-361,-324,-283,-240,
  108.    -195,-148,-99,-49
  109.    };
  110.  
  111. /***
  112.  *
  113.  *
  114.  * setting up effects/doing effects.
  115.  * The set_xxx gets called while parsing the effect,
  116.  * the do_xxx gets called each tick, and update the
  117.  * sound parameters while playing it.
  118.  *
  119.  *
  120.  ***/
  121.  
  122.  
  123. void do_nothing(ch)
  124. struct channel *ch;
  125.    {
  126.    }
  127.  
  128. LOCAL void set_nothing(a, ch)
  129. struct automaton *a;
  130. struct channel *ch;
  131.    {
  132.    }
  133.  
  134. /* slide pitch (up or down) */
  135. LOCAL void do_slide(ch)
  136. struct channel *ch;
  137.    {
  138.    ch->pitch += ch->slide;
  139.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  140.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  141.    set_current_pitch(ch, ch->pitch);
  142.    }
  143.  
  144. LOCAL void set_upslide(a, ch)
  145. struct automaton *a;
  146. struct channel *ch;
  147.    {
  148.    ch->adjust = do_slide;
  149.    if (a->para)
  150.       ch->slide = a->para;
  151.    }
  152.  
  153. LOCAL void set_downslide(a, ch)
  154. struct automaton *a;
  155. struct channel *ch;
  156.    {
  157.    ch->adjust = do_slide;
  158.    if (a->para)
  159.       ch->slide = -a->para;
  160.    }
  161.  
  162. /* modulating the pitch with vibrato */
  163. LOCAL void do_vibrato(ch)
  164. struct channel *ch;
  165.    {
  166.    int offset;
  167.  
  168.       /* this is no longer a literal transcription of the pt
  169.        * code. I have rescaled the vibrato table.
  170.        */
  171.    ch->viboffset += ch->vibrate;
  172.    ch->viboffset &= 63;
  173.       /* please don't use logical shift on signed values */
  174.    offset = (vibrato_table[ch->viboffset] * ch->vibdepth)/256;
  175.       /* temporary update of only the step value,
  176.        * note that we do not change the saved pitch.
  177.        */
  178.    set_current_pitch(ch, ch->pitch + offset);
  179.    }
  180.  
  181. LOCAL void set_vibrato(a, ch)
  182. struct automaton *a;
  183. struct channel *ch;
  184.    {
  185.    ch->adjust = do_vibrato;
  186.    if (HI(a->para))
  187.       ch->vibrate = HI(a->para);
  188.    if (LOW(a->para))
  189.       ch->vibdepth = LOW(a->para);
  190.    }
  191.  
  192. /* arpeggio looks a bit like chords: we alternate between two
  193.  * or three notes very fast.
  194.  */
  195. LOCAL void do_arpeggio(ch)
  196. struct channel *ch;
  197.    {
  198.    if (++ch->arpindex >= MAX_ARP)
  199.       ch->arpindex =0;
  200.    set_current_pitch(ch, ch->arp[ch->arpindex]);
  201.    }
  202.  
  203. LOCAL void set_arpeggio(a, ch)
  204. struct automaton *a;
  205. struct channel *ch;
  206.    {
  207.       /* arpeggio can be installed relative to the
  208.        * previous note, so we have to check that there
  209.        * actually is a current(previous) note
  210.        */
  211.    if (ch->note == NO_NOTE)
  212.       {
  213.       status("No note present for arpeggio");
  214.       error = FAULT;
  215.       }
  216.    else
  217.       {
  218.       int note;
  219.  
  220.       ch->arp[0] = pitch_table[ch->note][ch->finetune];
  221.       note = ch->note + HI(a->para);
  222.       if (note < NUMBER_NOTES)
  223.          ch->arp[1] = pitch_table[note][ch->finetune];
  224.       else
  225.          {
  226.          status("Arpeggio note out of range");
  227.          error = FAULT;
  228.          }
  229.       note = ch->note + LOW(a->para);
  230.       if (note < NUMBER_NOTES)
  231.          ch->arp[2] = pitch_table[note][ch->finetune];
  232.       else
  233.          {
  234.          status("Arpeggio note out of range");
  235.          error = FAULT;
  236.          }
  237.       ch->arpindex = 0;
  238.       ch->adjust = do_arpeggio;
  239.       }
  240.    }
  241.  
  242. /* volume slide. Mostly used to simulate waveform control.
  243.  * (attack/decay/sustain).
  244.  */
  245. LOCAL void do_slidevol(ch)
  246. struct channel *ch;
  247.    {
  248.    set_current_volume(ch, ch->volume + ch->volumerate);
  249.    }
  250.  
  251. /* note that volumeslide does not have a ``take default''
  252.  * behavior. If para is 0, this is truly a 0 volumeslide.
  253.  * Issue: is the test really necessary ? Can't we do
  254.  * a HI(para) - LOW(para). Answer: protracker does not.
  255.  */
  256. LOCAL void parse_slidevol(ch, para)
  257. struct channel *ch;
  258. int para;
  259.    {
  260.    if (LOW(para))
  261.       ch->volumerate = -LOW(para);
  262.    else
  263.       ch->volumerate = HI(para);
  264.    }
  265.  
  266. LOCAL void set_slidevol(a, ch)
  267. struct automaton *a;
  268. struct channel *ch;
  269.    {
  270.    ch->adjust = do_slidevol;
  271.    parse_slidevol(ch, a->para);
  272.    }
  273.  
  274. /* portamento: gets from a given pitch to another.
  275.  * We can simplify the routine by cutting it in
  276.  * a pitch up and pitch down part while setting up
  277.  * the effect.
  278.  */
  279. LOCAL void do_portamento(ch)
  280. struct channel *ch;
  281.    {
  282.    if (ch->pitch < ch->pitchgoal)
  283.       {
  284.       ch->pitch += ch->pitchrate;
  285.       ch->pitch = MIN(ch->pitch, ch->pitchgoal);
  286.       }
  287.    else if (ch->pitch > ch->pitchgoal)
  288.       {
  289.       ch->pitch -= ch->pitchrate;
  290.       ch->pitch = MAX(ch->pitch, ch->pitchgoal);
  291.       }
  292.       /* if we want to implement funk glissando, we need a change right
  293.        * there
  294.        */
  295.    set_current_pitch(ch, ch->pitch);
  296.    }
  297.  
  298. /* if para and pitch are 0, this is obviously a continuation
  299.  * of the previous portamento.
  300.  */
  301. LOCAL void set_portamento(a, ch)
  302. struct automaton *a;
  303. struct channel *ch;
  304.    {
  305.    ch->adjust = do_portamento;
  306.    if (a->para)
  307.       ch->pitchrate = a->para;
  308.    if (a->pitch)
  309.       ch->pitchgoal = a->pitch;
  310.    }
  311.  
  312. /*
  313.  * combined commands.
  314.  */
  315. LOCAL void do_portaslide(ch)
  316. struct channel *ch;
  317.    {
  318.    do_portamento(ch);
  319.    do_slidevol(ch);
  320.    }
  321.  
  322. LOCAL void set_portaslide(a, ch)
  323. struct automaton *a;
  324. struct channel *ch;
  325.    {
  326.    ch->adjust = do_portaslide;
  327.    if (a->pitch)
  328.       ch->pitchgoal = a->pitch;
  329.    parse_slidevol(ch, a->para);
  330.    }
  331.  
  332. LOCAL void do_vibratoslide(ch)
  333. struct channel *ch;
  334.    {
  335.    do_vibrato(ch);
  336.    do_slidevol(ch);
  337.    }
  338.  
  339. LOCAL void set_vibratoslide(a, ch)
  340. struct automaton *a;
  341. struct channel *ch;
  342.    {
  343.    ch->adjust = do_vibratoslide;
  344.    parse_slidevol(ch, a->para);
  345.    }
  346.  
  347. /***
  348.  *
  349.  *  effects that just need a setup part
  350.  *
  351.  ***/
  352.  
  353. /* IMPORTANT: because of the special nature of
  354.  * the player, we can't process each effect independently,
  355.  * we have to merge effects from the four channel before
  356.  * doing anything about it. For instance, there can be 
  357.  * several speed change in the same note.
  358.  */
  359. LOCAL void set_speed(a, ch)
  360. struct automaton *a;
  361. struct channel *ch;
  362.    {
  363.    if (a->para >= 32)
  364.       {
  365.       a->new_finespeed = a->para;
  366.       a->do_stuff |= SET_FINESPEED;
  367.       }
  368.    else if (a->para)
  369.       {
  370.       a->new_speed = a->para;
  371.       a->do_stuff |= SET_SPEED;
  372.       }
  373.    }
  374.  
  375. LOCAL void set_skip(a, ch)
  376. struct automaton *a;
  377. struct channel *ch;
  378.    {
  379.       /* BCD decoding in read.c */
  380.    a->new_note = a->para;
  381.    a->do_stuff |= SET_SKIP;
  382.    }
  383.  
  384. LOCAL void set_fastskip(a, ch)
  385. struct automaton *a;
  386. struct channel *ch;
  387.    {
  388.    a->new_pattern = a->para;
  389.    a->do_stuff |= SET_FASTSKIP;
  390.    }
  391.  
  392. /* immediate effect: starts the sample somewhere
  393.  * off the start.
  394.  */
  395. LOCAL void set_offset(a, ch)
  396. struct automaton *a;
  397. struct channel *ch;
  398.    {
  399.    set_position(ch, a->para * 256);
  400.    }
  401.  
  402. /* change the volume of the current channel.
  403.  * Is effective until there is a new set_volume,
  404.  * slide_volume, or an instrument is reloaded 
  405.  * explicitly by giving its number. Obviously, if
  406.  * you load an instrument and do a set_volume in the
  407.  * same note, the set_volume will take precedence.
  408.  */
  409. LOCAL void set_volume(a, ch)
  410. struct automaton *a;
  411. struct channel *ch;
  412.    {
  413.    set_current_volume(ch, a->para);
  414.    }
  415.  
  416.  
  417.  
  418. /***
  419.  *
  420.  * EXTENDED COMMANDS
  421.  *
  422.  ***/
  423.  
  424. /* extended command: retrig note at a fast pace
  425.  */
  426. LOCAL void do_retrig(ch)
  427. struct channel *ch;
  428.    {
  429.    if (--ch->current <= 0)
  430.       {
  431.       reset_note(ch, ch->note, ch->pitch);
  432.       ch->current = ch->retrig;
  433.       }
  434.    }
  435.  
  436. LOCAL void set_retrig(a, ch)
  437. struct automaton *a;
  438. struct channel *ch;
  439.    {
  440.    ch->retrig = a->para;
  441.    ch->current = ch->retrig;
  442.    ch->adjust = do_retrig;
  443.    }
  444.  
  445. /* extended command: start note after a small
  446.  * delay
  447.  */
  448. LOCAL void do_latestart(ch)
  449. struct channel *ch;
  450.    {
  451.    if (--ch->current <= 0)
  452.       {
  453.       reset_note(ch, ch->note, ch->pitch);
  454.       ch->adjust = do_nothing;
  455.       }
  456.    }
  457.  
  458. LOCAL void set_late_start(a, ch)
  459. struct automaton *a;
  460. struct channel *ch;
  461.    {
  462.    play_note(ch->audio, NULL, 0);
  463.    ch->current = a->para;
  464.    ch->adjust = do_latestart;
  465.    }
  466.  
  467. /* extended command: cut note after some time.
  468.  * Note we only kill the volume, as protracker does...
  469.  */
  470. LOCAL void do_cut(ch)
  471. struct channel *ch;
  472.    {
  473.    if (ch->retrig)
  474.       {
  475.       if (--ch->retrig == 0)
  476.          set_current_volume(ch, 0);
  477.       }
  478.    }
  479.  
  480. LOCAL void set_note_cut(a, ch)
  481. struct automaton *a;
  482. struct channel *ch;
  483.    {
  484.    ch->retrig = a->para;
  485.    ch->adjust = do_cut;
  486.    }
  487.  
  488.  
  489. LOCAL void set_smooth_up(a, ch)
  490. struct automaton *a;
  491. struct channel *ch;
  492.    {
  493.    ch->pitch += a->para;
  494.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  495.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  496.    set_current_pitch(ch, ch->pitch);
  497.    }
  498.  
  499. LOCAL void set_smooth_down(a, ch)
  500. struct automaton *a;
  501. struct channel *ch;
  502.    {
  503.    ch->pitch -= a->para;
  504.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  505.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  506.    set_current_pitch(ch, ch->pitch);
  507.    }
  508.  
  509. LOCAL void set_change_finetune(a, ch)
  510. struct automaton *a;
  511. struct channel *ch;
  512.    {
  513.    ch->finetune = a->para;
  514.    }
  515.  
  516.  
  517. LOCAL void set_loop(a, ch)
  518. struct automaton *a;
  519. struct channel *ch;
  520.    {
  521.       /* Note: the current implementation of protracker
  522.        * does not allow for a jump from pattern to pattern,
  523.        * even though it looks like a logical extension to the current 
  524.        * format.
  525.        */
  526.    if (a->para == 0) 
  527.       a->loop_note_num = a->note_num;
  528.    else
  529.       {
  530.       if (a->loop_counter == 0)
  531.          a->loop_counter = a->para + 1;
  532.       /* We have to defer the actual count-down and note jump
  533.        * to automaton.c, because some modules include several
  534.        * loops on the same measure, which is a bit confusing
  535.        * (see don't you want me)
  536.        */
  537.       a->do_stuff |= JUMP_PATTERN;
  538.       }
  539.    }
  540.  
  541. LOCAL void set_smooth_upvolume(a, ch)
  542. struct automaton *a;
  543. struct channel *ch;
  544.    {
  545.    set_current_volume(ch, ch->volume + a->para);
  546.    }
  547.  
  548. LOCAL void set_smooth_downvolume(a, ch)
  549. struct automaton *a;
  550. struct channel *ch;
  551.    {
  552.    set_current_volume(ch, ch->volume - a->para);
  553.    }
  554.  
  555.  
  556. LOCAL void set_delay_pattern(a, ch)
  557. struct automaton *a;
  558. struct channel *ch;
  559.    {
  560.    a->counter -= (a->para + 1) * a->speed;
  561.    a->do_stuff |= DELAY_PATTERN;
  562.    }
  563.  
  564.  
  565.  
  566. /* Initialize the whole effect table */
  567.  
  568. void init_effects(table)
  569. void (*table[]) P((struct automaton *a, struct channel *ch));
  570.    {
  571.    int i;
  572.  
  573.    for (i = 0; i < NUMBER_EFFECTS; i++)
  574.       table[i] = set_nothing;
  575.    table[EFF_ARPEGGIO] = set_arpeggio;
  576.    table[EFF_SPEED] = set_speed;
  577.    table[EFF_SKIP] = set_skip;
  578.    table[EFF_FF] = set_fastskip;
  579.    table[EFF_VOLUME] = set_volume;
  580.    table[EFF_VOLSLIDE] = set_slidevol;
  581.    table[EFF_OFFSET] = set_offset;
  582.    table[EFF_PORTA] = set_portamento;
  583.    table[EFF_PORTASLIDE] = set_portaslide;
  584.    table[EFF_UP] = set_upslide;
  585.    table[EFF_DOWN] = set_downslide;
  586.    table[EFF_VIBRATO] = set_vibrato;
  587.    table[EFF_VIBSLIDE] = set_vibratoslide;
  588.    table[EFF_SMOOTH_UP] = set_smooth_up;
  589.    table[EFF_SMOOTH_DOWN] = set_smooth_down;
  590.    table[EFF_CHG_FTUNE] = set_change_finetune;
  591.    table[EFF_LOOP] = set_loop;
  592.    table[EFF_RETRIG] = set_retrig;
  593.    table[EFF_S_UPVOL] = set_smooth_upvolume;
  594.    table[EFF_S_DOWNVOL] = set_smooth_downvolume;
  595.    table[EFF_NOTECUT] = set_note_cut;
  596.    table[EFF_LATESTART] = set_late_start;
  597.    table[EFF_DELAY] = set_delay_pattern;
  598.    }
  599.  
  600.